#include <string.h>
#include <time.h>
#include <windows.h>
#include <stdio.h>


#define MI_START 1923
#define MI_STOP 1924
#define MI_EXIT 1925


int RUNNING = 0;
double SPEED = 15;
clock_t lastUpdated = 0;
const char ClassName[] = "MyWindowClass";

void CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
	RECT clientRect;

	GetClientRect(hwnd, &clientRect);
	InvalidateRect(hwnd, &clientRect, TRUE);
	return;
}

void CreateMyMenu(hwnd)
{
	HMENU hMenu = CreateMenu();
	HMENU hPopupMenu = CreatePopupMenu();
	AppendMenu(hPopupMenu, MF_STRING, MI_START, "Start");
	AppendMenu(hPopupMenu, MF_STRING, MI_STOP, "Stop");
	AppendMenu(hPopupMenu, MF_STRING, MI_EXIT, "Exit");
	AppendMenu(hMenu, MF_POPUP, (UINT)hPopupMenu, "File");
	SetMenu(hwnd, hMenu);
}

void RedrawText(HWND hwnd)
{
	RECT winRect, textRect;
	PAINTSTRUCT paintInfo;

	HDC hdc = BeginPaint(hwnd, &paintInfo);
	GetWindowRect(hwnd, &winRect);

	int width;
	LPSTR text = "The line is running away!";
	/*SIZE textSize;*/
	/*GetTextExtentPoint32(hdc, text, 26, &SIZE);*/
	clock_t now;
	static double top;
	static double left = 0;

	if (lastUpdated == 0)
	{
		if (RUNNING)
		{
			left = (winRect.right - winRect.left) / 2;
			top = 1.0/3 * (winRect.bottom - winRect.top);
			lastUpdated = clock();
		}
	}
	else
	{
		now = clock();
		printf("%d\n", now);
		width = winRect.right - winRect.left;
		if (RUNNING)
			left += SPEED * (now - lastUpdated) / CLK_TCK;
		top = 1.0/3 * (winRect.bottom - winRect.top);
		if (left >= width)
			left = 0;
		lastUpdated = now;
	}

	if (lastUpdated)
	{
		textRect.left = left;
		textRect.top = top;
		TextOut(hdc, textRect.left, textRect.top, text, strlen(text));
	}

	EndPaint(hwnd, &paintInfo);
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;

	switch(msg)
	{
		case WM_COMMAND:
			{
				wmId    = LOWORD(wParam);
				switch(wmId)
				{
					case MI_START:
						RUNNING = 1;
						break;
					case MI_STOP:
						RUNNING = 0;
						break;
					case MI_EXIT:
						DestroyWindow(hwnd);
					default:
						return DefWindowProc(hwnd, msg, wParam, lParam);
				}
			}
		case WM_PAINT:
			{
				RedrawText(hwnd);
				break;
			}
		case WM_CLOSE:
			DestroyWindow(hwnd);
			break;
		case WM_DESTROY:
			PostQuitMessage(0);		// this message will cause GetMessage return FALSE
			break;
		default:
			return DefWindowProc(hwnd, msg, wParam, lParam);
	}
	return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
		LPSTR lpCmdLine, int nCmdShow)
{
	WNDCLASSEX wc;
	HWND hwnd;
	MSG Msg;

	wc.cbSize = sizeof(WNDCLASSEX);		// size of the structure
	wc.style = 0;						// class style, usually can be set to 0
	wc.lpfnWndProc = WndProc;			// pointer to the window procedure
	wc.cbClsExtra = 0;					// amount of extra data allocated for this class in RAM
	wc.cbWndExtra = 0;					// amount of extra data per window of this class
	wc.hInstance = hInstance;			// handle to application instance
	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);		// large icon (when user presses Alt+Tab
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);		// cursor over windows of this class
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);	// background brush
	wc.lpszMenuName = NULL;				// name of a menu resource
	wc.lpszClassName = ClassName;	// name to identify the class with
	wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);	// small icon

	if(!RegisterClassEx(&wc))
	{
		MessageBox(NULL, "Window Registration Failed!", "Error!",
				MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}


	hwnd = CreateWindowEx(
			WS_EX_CLIENTEDGE,						// extended windows style
			ClassName,							// class name
			"Running!",								// window title
			WS_OVERLAPPEDWINDOW,					// window style
			CW_USEDEFAULT, CW_USEDEFAULT, 320, 240,	// topleft, width, height
			NULL, NULL, hInstance, NULL);			// parent window, menu, app instance handles and a pointer to the window creation data

	if (hwnd == NULL)
	{
		MessageBox(NULL, "Window Creation Failed!", "Error!",
				MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	CreateMyMenu(hwnd);
	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);

	UINT htimer = SetTimer(hwnd, 1, 100, TimerProc);

	while(GetMessage(&Msg, NULL, 0, 0) > 0)
	{
		TranslateMessage(&Msg);		// some additional processing on keyboard events
		DispatchMessage(&Msg);		// dispatch the message to the right window
	}
	KillTimer(hwnd, htimer);
	return Msg.wParam;		// value passed to PostQuitMessage
}
